package com.wyh.utils.sqlUtils.activity;

import com.wyh.entity.Activity;

import javax.validation.constraints.NotNull;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

public class addActivitySqlDynamic {
    public String addDynamic(@NotNull Activity activity)
    {
        StringBuilder builder = new StringBuilder();
        StringBuilder parameterPlaceholders = new StringBuilder();
        List<String> parameters = new ArrayList<>();
        if (Objects.isNull(activity) || Objects.isNull(activity.getId())){
            if (builder.length() > 0) {
                builder.append(", ");
                parameterPlaceholders.append(", ");
            }
            builder.append("id");
            parameterPlaceholders.append("#{id}");
            parameters.add("id");
        }

        if (activity.getName() != null) {
            if (builder.length() > 0) {
                builder.append(", ");
                parameterPlaceholders.append(", ");
            }
            builder.append("name");
            parameterPlaceholders.append("#{name}");
            parameters.add("name");
        }
        if (activity.getType() != null) {
            if (builder.length() > 0) {
                builder.append(", ");
                parameterPlaceholders.append(", ");
            }
            builder.append("type");
            parameterPlaceholders.append("#{type}");
            parameters.add("type");
        }
        if (activity.getTime() != null) {
            if (builder.length() > 0) {
                builder.append(", ");
                parameterPlaceholders.append(", ");
            }
            builder.append("time");
            parameterPlaceholders.append("#{time}");
            parameters.add("time");
        }
        if (activity.getAddress() != null) {
            if (builder.length() > 0) {
                builder.append(", ");
                parameterPlaceholders.append(", ");
            }
            builder.append("address");
            parameterPlaceholders.append("#{address}");
            parameters.add("address");
        }
        if (activity.getOrganizer_user_id_fk() != null) {
            if (builder.length() > 0) {
                builder.append(", ");
                parameterPlaceholders.append(", ");
            }
            builder.append("organizer_user_id_fk");
            parameterPlaceholders.append("#{organizer_user_id_fk}");
            parameters.add("organizer_user_id_fk");
        }
        if (activity.getRemark() != null) {
            if (builder.length() > 0) {
                builder.append(", ");
                parameterPlaceholders.append(", ");
            }
            builder.append("remark");
            parameterPlaceholders.append("#{remark}");
            parameters.add("remark");
        }

        StringBuilder sql = new StringBuilder();
        sql.append("INSERT INTO Activity (");
        sql.append(builder);
        sql.append(") VALUES (");
        sql.append(parameterPlaceholders);
        sql.append(");");

        System.out.println(sql);
        return sql.toString();
    }
}
